/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lossy;

import java.awt.image.BufferedImage;
import static lossy.Threshold.getBlue;
import static lossy.Threshold.getGreen;
import static lossy.Threshold.getRed;

/**
 *
 * @author Atuan
 */
public class LowPassFilter {
    
    public static double[][] performLowPassFilter(double[][] in, int n){
        double[][] out = in;
		BufferedImage image = new BufferedImage(3,3,BufferedImage.TYPE_INT_RGB);
                
		for(int i = 1; i < n-1; i++){
			for(int j = 1; j < n-1; j++){
//                            image.setRGB(0, 0, (int)in[i-1][j-1]);
//                            image.setRGB(0, 1, (int)in[i-1][j]);
//                            image.setRGB(1, 0, (int)in[i][j-1]);
//                            image.setRGB(1, 1, (int)in[i][j]);
//                            image.setRGB(0, 2, (int)in[i-1][j+1]);
//                            image.setRGB(2, 0, (int)in[i+1][j-1]);
//                            image.setRGB(2, 1, (int)in[i+1][j]);
//                            image.setRGB(1, 2, (int)in[i][j+1]);
//                            image.setRGB(2, 2, (int)in[i+1][j+1]);
//                            
//                            int c1 = (getRed(image.getRGB(0, 0))+getBlue(image.getRGB(0, 0))+getGreen(image.getRGB(0,0))/3);
//                            int c2 = (getRed(image.getRGB(0, 1))+getBlue(image.getRGB(0, 1))+getGreen(image.getRGB(0,1))/3);
//                            int c3 = (getRed(image.getRGB(1, 0))+getBlue(image.getRGB(1, 0))+getGreen(image.getRGB(1,0))/3);
//                            int c4 = (getRed(image.getRGB(1, 1))+getBlue(image.getRGB(1, 1))+getGreen(image.getRGB(1,1))/3);
//                            int c5 = (getRed(image.getRGB(0, 2))+getBlue(image.getRGB(0, 2))+getGreen(image.getRGB(0,2))/3);
//                            int c6 = (getRed(image.getRGB(2, 0))+getBlue(image.getRGB(2, 0))+getGreen(image.getRGB(2,0))/3);
//                            int c7 = (getRed(image.getRGB(2, 1))+getBlue(image.getRGB(2, 1))+getGreen(image.getRGB(2,1))/3);
//                            int c8 = (getRed(image.getRGB(1, 2))+getBlue(image.getRGB(1, 2))+getGreen(image.getRGB(1,2))/3);
//                            int c9 = (getRed(image.getRGB(2, 2))+getBlue(image.getRGB(2, 2))+getGreen(image.getRGB(2,2))/3);
//                            int color = (int)((c1+c2+c3+c4+c5+c6+c7+c8+c9)/9);
//                            out[i][j] = mixColor(color,color,color);
//                            out[i][j] = (int) ((in[i-1][j-1]/9.0+in[i-1][j]/9.0+in[i-1][j+1]/9.0
//                                    +in[i][j]/9.0+in[i][j-1]/9.0+in[i][j+1]/9.0+
//                                    in[i+1][j-1]/9.0+in[i+1][j]/9.0+in[i+1][j+1]/9.0));
                            double d = 16.0;
                            out[i][j] = (int) ((in[i-1][j]/d+
                                    +(3*in[i][j])/4.0+
                                    in[i][j-1]/d+in[i][j+1]/d+
                                    in[i+1][j]/d));
//                            if((int)(Math.random()*789)==23)
//                            System.out.println(out[i][j]);
			}
		}
		
		return out;
    }
    
	private static int mixColor(int red, int green, int blue) {
		return red<<16|green<<8|blue;
	}

	public static int getRed(int color) {
		return (color & 0x00ff0000)  >> 16;
	}
	
	public static int getGreen(int color) {
		return	(color & 0x0000ff00)  >> 8;
	}
	
	public static int getBlue(int color) {
		return (color & 0x000000ff)  >> 0;
		
	}
}
